<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/23
 * Time: 10:23
 */
class ModRecordLog{

    public function addLog($table,$data)
    {
        $db = new LibDatabase();
        if ($db->insert($table, $data)) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * 获取当前时间一个小时之前的聊天日志，根据channel_id
     */
    public function queryChatLog($params){
        $db = new LibDatabase();
        $sql = "select * from log_chat ";
        $time = time() - 3600*2;
        $where = "where channel_id='{$params['room_id']}' and log_time>={$time} ";
        $sql .= $where;
        $sql .= " order by log_time asc ";
        $result = array();
        $result = $db->queryDataBySql($sql);
        $allUserImage = $this->getAllUserImageId();
        foreach($result as &$val){
            $val["time"] = date("H:i:s",$val["log_time"]);
            $val["image_id"] = empty($allUserImage[$val["user_name"]])?"1":$allUserImage[$val["user_name"]];
        }
        unset($val);
        return $result;
    }

    public function addUser($data){
        if($this->checkUser(trim($data["user_name"]))) {
            $params["user_name"] = $data["user_name"];
            $params["mail_address"] = $data["mail_address"];
            $params["psd"] = md5("@bB".$data["psd"]."4399aA@");
            $params["log_time"] = time();
            $max = $this->getMaxImageId();
            $max = $max+1;
            $params["image_id"] = $max;
            if($this->addLog("user",$params)){
                return true;
            }
        }
        return false;
    }

    public function checkUser($user_name){
        $sql = "select * from `user` ";
        $where = " where user_name='".$user_name."'";
        $sql .= $where;
        $db = new LibDatabase();
        $result = $db->queryDataBySql($sql);
        if($result){
            return false;
        }else{
            return true;
        }
    }

    public function checkLogin($data){
        $mail_address = $data["mail_address"];
        $password = md5("@bB".$data["psd"]."4399aA@");
        $sql = "select user_name from `user` ";
        $where = " where mail_address='".$mail_address."'"." and psd='".$password."'";
        $sql .= $where;
        $db = new LibDatabase();
        $result = $db->queryDataBySql($sql);
        if($result){
            return $result;
        }else{
            return false;
        }
    }

    public function initUserImage($user_name){
        //首先查询用户信息，判断这个用户的images是否为空，如果为空的话，则插入image_id
        $userInfo = $this->getUserImageId($user_name);
        if($userInfo["image_id"]){
            return true;
        }else{
            //update用户的image_id
            $max = $this->getMaxImageId();
            $max = $max+1;
            $sql = "update `user` set image_id=$max";
            $where = " where user_name='".$user_name."'";
            $sql .= $where;
            $db = new LibDatabase();
            $result = $db->updateBySql($sql);
            if($result){
                return true;
            }else{
                return false;
            }
        }
    }

    public function getUserImageId($user_name){
        $sql = "select user_name,image_id from `user` ";
        $where = " where user_name='".$user_name."'";
        $sql .= $where;
        $db = new LibDatabase();
        $result = $db->queryDataBySql($sql);
        return $result;
    }

    public function getAllUserImageId(){
        $sql = "select user_name,image_id from `user` ";
        $db = new LibDatabase();
        $tmp = $db->queryDataBySql($sql);
        $result = array();
        foreach($tmp as $val){
            $result[$val["user_name"]] = $val["image_id"];
        }
        return $result;
    }

    public function getMaxImageId(){
        $sql = "select max(image_id) as num from `user` limit 1";
        $db = new LibDatabase();
        $tmp = $db->queryDataBySql($sql);
        return $tmp[0]['num'];
    }
}